//
//  YYCollectionController.swift
//  YYProject
//
//  Created by 陆文宇 on 2017/8/16.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class YYCollectionController: YYController, UICollectionViewDelegate, UICollectionViewDataSource{
    
    var yy_collectionView: UICollectionView!
    var yy_collectionHeaderView: UIView!
    var yy_dataSource = [YYModel]()
    var yy_page = 1
    var yy_size = 10

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func yy_config() {
        super.yy_config()
        
        yy_createCollectionView()
    }
    
    final func yy_createCollectionView() {
        yy_collectionView = UICollectionView(frame: .zero, collectionViewLayout:yy_defaultCollectionViewLayout())
        yy_collectionView.backgroundColor = .white
        yy_collectionView.dataSource = self
        yy_collectionView.delegate = self
        yy_collectionView.mj_header = MJRefreshNormalHeader(refreshingClosure: { [weak self] in
            if let `self` = self {
                self.yy_page = 1
                self.yy_requestData()
            }
        })
        yy_collectionView.mj_footer = MJRefreshAutoNormalFooter(refreshingClosure: { [weak self] in
            if let `self` = self {
                self.yy_requestData()
            }
        })
        yy_collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collection_header")
        yy_collectionHeaderView = view
        yy_configCollectionView()
        view = yy_collectionView
        
        yy_collectionView.mj_header?.state = .refreshing
        yy_registeredCellClasses().forEach { (cellClass) in
            cellClass.yy_register(collectionView: yy_collectionView)
        }
    }
    
    func yy_configCollectionView() {
        yy_defaultCollectionViewConfig()
    }
    
    func yy_createCollectionViewLayout(defaultFlowLayout layout: UICollectionViewFlowLayout) -> UICollectionViewLayout {
        return layout
    }
    
    override func yy_requestData() {
        super.yy_requestData()
        
        yy_collectionView.mj_header?.endRefreshing()
        yy_collectionView.mj_footer?.endRefreshing()
    }
    
    override func yy_analyzData(_ data: Any) -> [YYModel]? {
        yy_collectionView.mj_header?.endRefreshing()
        return super.yy_analyzData(data)
    }
    
    override func yy_handlerData(_ data: [YYModel]?) {
        guard data != nil else {
            return
        }
        
        if yy_page == 1 {
            yy_dataSource = data!
        } else {
            yy_dataSource.append(contentsOf: data!)
        }
        yy_collectionView.reloadData()
        yy_page += 1
        
        if data!.count < yy_size {
            yy_collectionView.mj_footer?.endRefreshingWithNoMoreData()
            
            if yy_dataSource.isEmpty {
                yy_collectionView.yy_showNoMoreData(with: "暂无数据")
            } else {
                yy_collectionView.yy_dismissNoMoreData()
            }
        } else {
            yy_collectionView.yy_dismissNoMoreData()
            yy_collectionView.mj_footer?.endRefreshing()
        }
    }
    
    override func yy_requestFailure(errmsg: String) -> String? {
        yy_collectionView.mj_header?.endRefreshing()
        yy_collectionView.mj_footer?.endRefreshing()
        return super.yy_requestFailure(errmsg: errmsg)
    }
    
    func yy_registeredCellClasses() -> [YYCollectionCell.Type] {
        return [YYCollectionCell.self]
    }
    
    func yy_dequeueReusableCellClass(indexPath: IndexPath) -> YYCollectionCell.Type {
        return YYCollectionCell.self
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return yy_dataSource.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = yy_dequeueReusableCellClass(indexPath: indexPath).yy_dequeueReusableCell(collectionView: collectionView, indexPath: indexPath)
        cell.yy_model = yy_dataSource[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collection_header", for: indexPath)
        yy_collectionHeaderView.frame.size = (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).headerReferenceSize
        view.addSubview(yy_collectionHeaderView)
        return view
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
